//
//  ZJTaskAccountVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/11.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJTaskAccountVC.h"
#import "ZJTaskModel.h"
#import "ZJTaskCell.h"
#import "ZJRegistrationInformationConroller.h"
#import "ZJCommentVC.h"
#import "ZJSignUpInAdvanceController.h"
#import "ZJOnlinePayHouseController.h"
#import "ZJTuiGuangShuoMingVC.h"
@interface ZJTaskAccountVC ()
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UILabel *taskMoney;
@property (strong, nonatomic) ZJProjectDetailModel * proModel;
@property (weak, nonatomic) IBOutlet UIView *headView;
/**<用户信息>*/
@property (nonatomic,strong) ZJUserModel *model;
/**<模型数组>*/
@property (nonatomic,strong) NSArray *modelArr;
@end

@implementation ZJTaskAccountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headView.backgroundColor = [Utils colorWithHexString:allConstHexColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZJTaskCell class]) bundle:nil] forCellReuseIdentifier:@"ZJTaskCell"];
    [self.tableview setSeparatorColor:[Utils colorWithHexString:@"dddddd"]];
    [self sendRequest];
}

#pragma mark - ******************请求数据
-(void)sendRequest{
    [HUD showLoading:@"加载数据中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{}];
    NSString *url = [LRNetwork requestURL:@"cgi/mission_list.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        self.modelArr = [ZJTaskModel mj_objectArrayWithKeyValuesArray:returnValue[@"mission"]];
        self.model = GET_USERINFO;
        self.proModel = GET_PROJECTINFO;
        self.taskMoney.text = self.model.money_mission;
        [self.tableView reloadData];
        [HUD hide];
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWitMsg:kNoDataImgName ifNecessaryForRowCount:self.modelArr.count];
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZJTaskCell"];
    ZJTaskModel * model = self.modelArr[indexPath.row];
    cell.sureBtn.tag = indexPath.row;
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    [cell.sureBtn addTarget:self action:@selector(sureBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
-(void)sureBtn:(UIButton *)sender{
    ZJTaskModel * model = self.modelArr[sender.tag];
    
    if (![self.model.is_sincere isEqualToString:@"1"] && [model.only_sincere isEqualToString:@"1"]) {
        
        [HUD showMsg:@"此任务仅针对提前报名用户"];
        
    }else{
        NSDictionary *parameter = [LRNetwork requestParameter:@{@"mission_id":@""}];
        
        ZJRegistrationInformationConroller *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJRegistrationInformationConroller"];
        vc.isBackRoot = NO;
        
        ZJCommentVC *vc1 = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJCommentVC"];
        
        ZJSignUpInAdvanceController *vc2 = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSignUpInAdvanceController"];
        
        ZJOnlinePayHouseController *vc3 = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJOnlinePayHouseController"];
        switch ([model.mission_code integerValue]) {
            case 1000:
                [parameter setValue:model.mission_id forKey:@"mission_id"];
                break;
                
            case 1001:
                
                [self.navigationController pushViewController:vc animated:YES];
                break;
                
            case 1003:
                [self.navigationController pushViewController:vc1 animated:YES];
                break;
                
            case 1004:
                {
                    ZJTuiGuangShuoMingVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJTuiGuangShuoMingVC"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                break;
                
            case 1005:
                [parameter setValue:model.mission_id forKey:@"mission_id"];
                break;
                
            case 1006:
                [parameter setValue:model.mission_id forKey:@"mission_id"];
                break;
                
            case 1007:
                [self.navigationController pushViewController:vc2 animated:YES];
                break;
                
            case 1008:
                {
                    vc3.prModel = self.proModel;
                    [self.navigationController pushViewController:vc3 animated:YES];
                }
                break;
                
            default:
                break;
        }
        
        if (![parameter[@"mission_id"] isEqualToString:@""]) {
            
            [HUD showMsg:@"提交中..."];
            NSString *url = [LRNetwork requestURL:@"cgi/mission.php"];
            
            [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
                double misMoney = [self.model.money_mission floatValue] + [model.money floatValue];
                self.model.money_mission = [NSString stringWithFormat:@"%.2f",misMoney];
                self.model.money_total = [NSString stringWithFormat:@"%.2f",[self.model.money_total floatValue] + [model.money floatValue]];
                SYN_USER(self.model);
                [self sendRequest];
                
            } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
                [HUD showErrorMsg:errorMessage];
            } withFailureBlock:^{
                
            }];
        }

    }

}
#pragma mark - 补全分隔线左侧缺失
- (void)viewDidLayoutSubviews {
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableview setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

@end
